package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import java.util.List;

import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.object.ObjectMetaLayoutQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.object.ObjectMetaLayoutVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.object.ObjectMetaQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.object.ObjectMetaVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.ObjectComponentRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lich
 * @date 2021/11/11
 */
@RestController
@RequestMapping("/object")
@Api(value = "对象组件相关API", tags = {"对象组件相关API"})
@Slf4j
public class ObjectMetaController {

    @Autowired
    private ObjectComponentRepository objectComponentRepository;

    @ApiOperation(value = "通过对象类型的code，查询对象类型")
    @PostMapping("/metas/{code}")
    public ResponseResult<ObjectMetaVo> queryObjectMeta(@PathVariable String code) {
        ObjectMetaQueryVo queryVo = new ObjectMetaQueryVo();
        queryVo.setCode(code);
        try {
            ObjectMetaVo objectMetaVo = objectComponentRepository.queryObjectMeta(queryVo);
            return ResponseResult.success(objectMetaVo);
        } catch (Exception ex) {
            log.error("查询对象类型有问题", ex);
            return ResponseResult.fail(
                ErrorCode.OBJECT_META_PROCESS_QUERY_FAIL.errCode(),
                ErrorCode.OBJECT_META_PROCESS_QUERY_FAIL.errMessage() + ":" + ex.getMessage());
        }
    }

    @ApiOperation(value = "通过对象类型的code和版本，查询类型布局")
    @PostMapping("/metas-layout")
    public ResponseResult queryObjectMetaLayout(@RequestParam String metaCode,
                                                @RequestParam Integer metaVersion) {
        ObjectMetaLayoutQueryVo queryVo = new ObjectMetaLayoutQueryVo();
        queryVo.setMetaCode(metaCode);
        queryVo.setMetaVersion(metaVersion);
        try {
            List<ObjectMetaLayoutVo> layoutVoList = objectComponentRepository.queryObjectMetaLayoutList(queryVo);
            return ResponseResult.success(layoutVoList);
        } catch (Exception ex) {
            log.error("查询对象类型布局有问题", ex);
            return ResponseResult.fail(
                ErrorCode.OBJECT_META_PROCESS_QUERY_FAIL.errCode(),
                ErrorCode.OBJECT_META_PROCESS_QUERY_FAIL.errMessage() + ":" + ex.getMessage());
        }
    }

}
